<template>
	<view class="main">
		<view class="emptyBox">
			<view class="button" @click="emptyCart">
				<u-icon name="trash" color="#82848a" size="18"></u-icon>
				&nbsp;清空
			</view>
		</view>
		<view class="productListBox" v-for="item in userSelectProductInfoList" :key="item._id">
			<view class="leftBox">
				<image :src="item.picurls" mode="aspectFill"></image>
			</view>
			<view class="rightBox">
				<view class="name">{{ item.name }}</view>
				<view class="price_count">
					<view class="price">￥{{ item.price }}/{{ item.unit }}</view>
					<u-number-box integer step="1" min="0" max="999" v-model="item.selectNumber"></u-number-box>
				</view>
			</view>
		</view>
		<view class="settlementBox">
			<view class="totalPriceBox">
				合计
				<text class="price">￥{{ calcTotalPrice.totalPrice }}</text>
				<text class="count">（共{{ calcTotalPrice.productCount }}件，不含运费）</text>
			</view>
			<view class="settlementBut">结算</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			value: 0,
			userSelectProductInfoList: []
		};
	},
	computed: {
		calcTotalPrice() {
			let totalPrice = 0;
			let productCount = 0;
			this.userSelectProductInfoList.forEach((product) => {
				totalPrice += product.price * product.selectNumber;
				productCount += product.selectNumber;
			});
			return {
				totalPrice,
				productCount
			};
		}
	},
	onBackPress() {
		uni.setStorageSync('userSelectProductInfoList', this.userSelectProductInfoList);
	},
	onReady() {
		this.initOrderDetails();
	},
	methods: {
		emptyCart() {
			uni.removeStorageSync('userSelectProductInfoList');
			this.userSelectProductInfoList = [];
			uni.showToast({
				title: '购物车已清空!'
			});
		},
		initOrderDetails() {
			this.userSelectProductInfoList = uni.getStorageSync('userSelectProductInfoList');
		}
	}
};
</script>

<style lang="scss" scoped>
page {
	background-color: #f4f4f4;
}
.main {
	padding-bottom: 100rpx;
	.emptyBox {
		width: 750rpx;
		height: 50rpx;
		background-color: white;
		border-bottom: 1rpx solid #efeff0;
		box-sizing: border-box;

		.button {
			width: 100rpx;
			height: 100%;
			color: #82848a;
			font-size: 24rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-left: auto;
			padding-right: 20rpx;
		}
	}
	.productListBox {
		background-color: white;
		border-bottom: 1rpx solid #efeff0;
		width: 750rpx;
		height: 220rpx;
		display: flex;
		padding: 20rpx;
		box-sizing: border-box;
		align-items: center;

		.leftBox {
			// border: #82848a 1rpx solid;
			width: 180rpx;
			height: 180rpx;
			margin-right: 15rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
		.rightBox {
			flex: 1;
			height: 160rpx;
			// border: darkgreen 1rpx solid;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.name {
				// border: skyblue 1rpx solid;
			}
			.price_count {
				.price {
					display: flex;
					align-items: center;
					color: #fa3534;
				}
				// border: red 1rpx solid;
				display: flex;
				justify-content: space-between;
			}
		}
	}
	.settlementBox {
		background-color: white;
		width: 750rpx;
		height: 100rpx;
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		.totalPriceBox {
			flex: 1;
			height: 100%;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			color: #82848a;
			padding-left: 12rpx;
			.price {
				color: #fa3534;
				font-weight: bold;
			}
			.count {
				font-size: 23rpx;
			}
		}
		.settlementBut {
			background-color: #fa3534;
			width: 300rpx;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			border-top-left-radius: 100rpx;
			border-bottom-left-radius: 100rpx;
			color: white;
			font-size: 40rpx;
		}
	}
}
</style>
